package com.yykj.prescription.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalObjectUtil;
import com.yykj.prescription.client.dto.PrescriptionTipsDTO;
import com.yykj.prescription.server.converter.PrescriptionTipsDTOConverter;
import com.yykj.prescription.server.model.entity.setting.PrescriptionTips;
import com.yykj.prescription.server.model.repository.setting.PrescriptionTipsRepository;
import com.yykj.prescription.server.service.PrescriptionTipsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrescriptionTipsServiceImpl implements PrescriptionTipsService {
    @Override
    public PrescriptionTips editPrescriptionTips(PrescriptionTips prescriptionTips) {
        PrescriptionTips tip ;
        if(prescriptionTips.getId()!=null && prescriptionTips.getId() > 0){
            tip = repository.findById(prescriptionTips.getId()).orElseThrow(()->new NormalException(ResultEnums.NOT_EXIST));
        }else{
            tip = new PrescriptionTips();
        }
        LocalObjectUtil.copyNullValue(prescriptionTips,tip);
        tip =  repository.save(tip);
        return tip;
    }

    @Override
    public void deletePrescriptionTips(Integer tipId) {
        PrescriptionTips  tip = repository.findById(tipId).orElseThrow(()->new NormalException(ResultEnums.NOT_EXIST));
        repository.delete(tip);
    }

    @Override
    public List<PrescriptionTipsDTO>  listHospitalPrescriptionTips(Integer hospitalId) {
        List<PrescriptionTips> tips =  repository.findAllByHospitalIdOrderBySorting(hospitalId);
        return PrescriptionTipsDTOConverter.convert(tips);
    }
    private PrescriptionTipsRepository repository;

    @Autowired
    public void setRepository(PrescriptionTipsRepository repository) {
        this.repository = repository;
    }
}
